package com.ruoyi.well.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.well.domain.MqttControlInfo;
import com.ruoyi.well.mqtt.MqttPushClient;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("机井远程控制类")
@Slf4j
@RestController
@RequestMapping("/mqtt/well")
public class WellMqttController {

    @Autowired
    private MqttPushClient mqttPushClient;
    @GetMapping("/sendMsg")
    @ResponseBody
    public AjaxResult sendMsg() {
        return mqttPushClient.publish(0, false, "test01", "hello world  !!");
    }

    @PostMapping("/remoteControl")
    public AjaxResult remoteControl(MqttControlInfo mqttControlInfo){

        String deviceId = mqttControlInfo.getDeviceId();
        Integer onOff = mqttControlInfo.getOnOff();
        String content = onOff==1?"deviceOn":"deviceOff";
        String publishMessage = String.format("{\"type\":\"content\",\"%s\"}",content);
        return mqttPushClient.publish(0,false,"report",publishMessage);
    }

}
